package cn.zifangsky.jvm.part1.structure.holder;

import javax.xml.ws.Holder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Holder测试2
 *
 * @author zifangsky
 * @date 2020/7/2
 * @since 1.0.0
 */
public class HolderTest3 {

    /**
     * 如果想要同时获取“低卡路里的菜肴名单，以及名单中的最低卡路里”，其他写法
     */
    public static void main(String[] args) {
        List<Dish> menuList = Arrays.asList(
                new Dish("猪肉", false, 800, Dish.Type.MEAT),
                new Dish("牛肉", false, 700, Dish.Type.MEAT),
                new Dish("鸡肉", false, 400, Dish.Type.MEAT),
                new Dish("薯条", true, 530, Dish.Type.OTHER),
                new Dish("米饭", true, 350, Dish.Type.OTHER),
                new Dish("水果", true, 120, Dish.Type.OTHER),
                new Dish("比萨饼", true, 550, Dish.Type.OTHER),
                new Dish("大虾", false, 300, Dish.Type.FISH),
                new Dish("龙利鱼", false, 450, Dish.Type.FISH) );

        //获取低卡路里的菜肴名单，以及名单中的最低卡路里
        Holder<Integer> minCalorieHolder = new Holder<>();
        List<Dish> lowCaloricDishes = new ArrayList<>();

        getLowCaloricDishes(menuList, lowCaloricDishes, minCalorieHolder);

        System.out.println(lowCaloricDishes);
        System.out.println("最低卡路里：" + minCalorieHolder.value);
    }

    /**
     * 获取低卡路里的菜肴名单，以及名单中的最低卡路里
     */
    private static void getLowCaloricDishes(List<Dish> menuList, List<Dish> lowCaloricDishes, Holder<Integer> minCalorieHolder){
        //1. 获取低卡路里的菜肴
        menuList.stream()
                //卡路里小于450
                .filter(m -> m.getCalories() < 450)
                .sorted(Comparator.comparing(Dish::getCalories))
                .forEach(lowCaloricDishes::add);

        //2. 获取名单中的最低卡路里
        int minCalorie = lowCaloricDishes.stream()
                .min(Comparator.comparing(Dish::getCalories))
                .get().getCalories();

        //3. 将最低卡路里赋值给对应的Holder
        minCalorieHolder.value = minCalorie;
    }

}
